package ca.uwo.android;

/*This comment is added*/
/* testing... commit */


import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.SearchRecentSuggestions;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import ca.uwo.android.alumni.AlumniMain;
import ca.uwo.android.askwestern.AskWestern;
import ca.uwo.android.campusmap.CampusMap;
import ca.uwo.android.directoryservice.DirectoryService;
import ca.uwo.android.mustangs.MustangsMain;
import ca.uwo.android.rss.events.RssDatesFeed;

import ca.uwo.android.util.DownloadTask;

import com.flurry.android.FlurryAgent;

public class AndroidWestern extends HomeScreenActivity {
	
	private static final String TAG_NAME = "AndroidWestern";
	
	// Alumni Events - alumni_events_feed
	private static final String URL4 = "http://pipes.yahoo.com/pipes/pipe.run?_id=dc1d097c6cef2b81ab1c6a98b661269c&_render=rss";
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
		        ca.uwo.android.directoryservice.SuggestionProvider.AUTHORITY, 
		        ca.uwo.android.directoryservice.SuggestionProvider.MODE);
		suggestions.clearHistory();
			
		GridView gridView = (GridView) findViewById(getGridViewId());
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				if(position == 0)
					startActivityFromChild(AndroidWestern.this, new Intent(AndroidWestern.this, DirectoryService.class), -1);
				else if(position == 1)
					startActivityFromChild(AndroidWestern.this, new Intent(AndroidWestern.this, CampusMap.class), -1);
				else if(position == 2){
					startEventCalendarActivity(getString(R.string.school_dates_feed), getString(R.string.sessionaldates_title), RssDatesFeed.SESSIONALDATES);
				}else if(position == 3){
					startNewsActivity(getString(R.string.western_gazette_feed), getString(R.string.gazettenews_title));
				}
				else if(position == 4){
					startNewsActivity(getString(R.string.western_news_feed), getString(R.string.westernnews_title));			
				}
				else if(position == 5){
					startNewsActivity(getString(R.string.usc_news_feed), getString(R.string.usc_news_title));
				}
				else if(position == 6){
					startEventCalendarActivity(getString(R.string.western_events_feed), getString(R.string.westernevents_title), RssDatesFeed.EVENTSCALENDAR);
				}
				else if(position == 7){
					startActivityFromChild(AndroidWestern.this, new Intent(AndroidWestern.this, AlumniMain.class), -1);
				}
				else if(position == 8){
					startActivityFromChild(AndroidWestern.this, new Intent(AndroidWestern.this, MustangsMain.class), -1);
				}
				else if(position == 9){
					FlurryAgent.logEvent(FlurryConstants.WL_MODULE_EVENT);
					startWebPageActivity(getString(R.string.library_main));
				}
				else if(position == 10){
					startEventCalendarActivity(getString(R.string.service_interruptions_feed), getString(R.string.serviceinterruptions_title), RssDatesFeed.SERVICEINTERRUPTIONS);
				}
				else if(position == 11){
					startActivityFromChild(AndroidWestern.this, new Intent(AndroidWestern.this, AskWestern.class), -1);
				}
				else if(position == 12){
					startNewsActivity(getString(R.string.media_relations_feed), getString(R.string.mediarelations_title));
				}else if(position == 13){
					FlurryAgent.logEvent(FlurryConstants.SAFECAMPUS_MODULE_EVENT);
					startWebPageActivity(getString(R.string.safecampus));
				}else if(position == 14){
					FlurryAgent.logEvent(FlurryConstants.MENTALHEALTH_MODULE_EVENT);
					startWebPageActivity(getString(R.string.mentalhealth));
				}else if(position == 15){
		
					startEventModulePrototype(getString(R.string.school_dates_feed),  			// academic events url
							                  getString(R.string.alumni_events_feed), 			// alumni events url
							                  getString(R.string.mustangs_schedule_link),		// mustangs events url
							                  getString(R.string.service_interruptions_feed),	// service interruptions url
							                  getString(R.string.western_events_feed));			// western events url
				}
				else {
					alert("Not yet implemented.", false);
				}
			}
			
		});
	}
	
    // Refreshes the display if the network connection and the
    // pref settings allow it.
    @Override
    public void onStart() {
        super.onStart();
		new DownloadTask(this.getPackageName()).execute(URL4);  
    }
	
	@Override
	protected int getContentViewId() {
		return R.layout.main;
	}


	@Override
	protected int getGridViewId() {
		return R.id.homeGridView;
	}


	@Override
	protected Integer[] getModuleIcons() {
		Integer[] moduleIcons = {
				R.drawable.directory_search,
				R.drawable.campus_map,
				R.drawable.academic_dates,
				R.drawable.gazettenews,
				R.drawable.western_news,
				R.drawable.usc_news,
				R.drawable.campus_events,
				R.drawable.alumni,
				R.drawable.mustangs,
				R.drawable.library_main,
				R.drawable.service_interruption,
				R.drawable.ask_western,
				R.drawable.media_relations,
				R.drawable.safe_campus,
				R.drawable.mental_health,
				R.drawable.campus_events
		};
		return moduleIcons;
	}


	@Override
	protected int getModuleNames() {
		return R.array.module_name_array;
	}
}